//
//  ALHomeViewController.m
//  Ummy.G
//
//  Created by 安林 on 16/9/27.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALHomeViewController.h"


@interface ALHomeViewController ()<UITableViewDataSource,UITableViewDelegate,TLCityPickerDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrM;

@end

@implementation ALHomeViewController

- (NSMutableArray *)arrM{
    
    if (_arrM == nil) {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor redColor];
    [self addItem];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //设置内边距
    self.tableView.contentInset= UIEdgeInsetsMake(-35, 0, 0, 0);
    [self addLoad];
    

    //进来就刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(addLoad)];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(addLoad)];
    
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_footer beginRefreshing];
//    [self.tableView reloadData];

}


#pragma mark 添加上面的导航栏
- (void)addItem{
    /*
     * 设置导航栏背景颜色
     */
    [self.navigationController.navigationBar setBarTintColor:ALColor(0, 201, 177)];
    
    //-----添加城市--------
    _city = [UIButton buttonWithType:UIButtonTypeCustom];
    [_city setTitle:@"石家庄" forState:UIControlStateNormal];
    
    [_city setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    
    //设置字体的大小
    _city.titleLabel.font = [UIFont systemFontOfSize:15];
    
    UIBarButtonItem * itemL = [UIBarButtonItem barButtonItemWith:_city addTarget:self action:@selector(cityClick) andImage:nil andHighlighted:nil];
    self.navigationItem.leftBarButtonItem = itemL;
    
    
    //-------二维码-------
    UIBarButtonItem *twocode = [UIBarButtonItem barButtonItemWith:[UIButton buttonWithType:UIButtonTypeCustom] addTarget:self action:@selector(sweep) andImage:@"icon_navigationItem_scan_white" andHighlighted:nil];
    //------消息---------
    UIBarButtonItem *message = [UIBarButtonItem barButtonItemWith:[UIButton buttonWithType:UIButtonTypeCustom] addTarget:self action:@selector(readMessage) andImage:@"icon_navigationItem_message_white" andHighlighted:nil];
    
    self.navigationItem.rightBarButtonItems =@[message,twocode];
    
    
    
    //-------搜索---------
    UIButton * search = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
    
    [search setBackgroundColor:[UIColor whiteColor]];
    
    [search.layer setMasksToBounds:YES];
    
    [search.layer setCornerRadius:15];
    
    [search.layer setBorderWidth:0.1];
    
    [search.layer setBorderColor:[UIColor whiteColor].CGColor];
    [search addTarget:self action:@selector(searchMessage) forControlEvents:UIControlEventTouchUpInside];
    //把搜索栏添加到中间的位置
    self.navigationItem.titleView = search;
    //添加那个搜索的小图标
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 15, 15)];
    [searchImage setImage:[UIImage imageNamed:@"icon_navigationItem_search"]];
    UILabel *placeHolder = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 170, 30)];
    placeHolder.font = [UIFont boldSystemFontOfSize:12];
    placeHolder.text = @"搜索商家、品类或商圈";
    placeHolder.textColor = [UIColor lightGrayColor];
    [search addSubview:placeHolder];
    [search addSubview:searchImage];
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }else if (section==1) {
//        self.tableView.sectionHeaderHeight =30;
        return 1;
    }else{
       return self.arrM.count;
    }
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * Ummy = nil;
    if (indexPath.section == 0) {
        static NSString *ID = @"First";
        
        ALFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [ALFirstTableViewCell loadALFirstTableViewCell];        }
        
        Ummy = cell;
    }else if (indexPath.section == 1) {
        static NSString *ID = @"Second";
        
        ALSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [ALSecondTableViewCell loadALSecondTableViewCell];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        Ummy = cell;
    }else if (indexPath.section == 2) {
        
        static NSString *ID = @"Third";
        

        ALThreeModel *model = self.arrM[indexPath.row];
        
        ALThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [ALThirdTableViewCell loadALThirdTableViewCell];
        }
        
        cell.model = model;
        Ummy = cell;
    }
    return Ummy;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 170;
    }else if (indexPath.section==1) {
        return 192;
    }else{
        return 100;
    }

}



- (void)addLoad{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    /*
    NSString *url = @"http://api.meituan.com/group/v2/recommend/homepage/city/76?userId=180479389&position=38.042167%2C114.477345&supportId=1&wifi-cur=0&wifi-mac=88%3A25%3A93%3A93%3A4e%3A9d%0888%3A25%3A93%3A93%3A4e%3A9c%0824%3A69%3A68%3Ae4%3Ae1%3A4f%0888%3A25%3A93%3A93%3A4e%3A1d%08&wifi-name=TP-LINK_1505%08TP-LINK_1505%2B%08TP-LINK_1503%08WYZC1506%08&wifi-strength=-44%08-54%08-52%08-65%08&fields=imageUrl%2Ctitle%2CimageTitle%2CsubTitle%2CsubTitle2%2CmainMessage%2CmainMessage2%2CsubMessage%2CtopRightInfo%2CbottomRightInfo%2C_type%2C_from%2C_id%2C_iUrl%2C_jumpNeed%2Ccolor%2Ccampaign%2CglobalId&client=android&__vhost=api.mobile.meituan.com&utm_source=huawei&utm_medium=android&utm_term=431&version_name=7.3.1&utm_content=a0000055de24e8&utm_campaign=AgroupBgroupC0D200E0&ci=76&msid=a0000055de24e81474794199082&uuid=FDC1C5960287691C1AA0FDB6CE8736766612182AEF7A556CA2D3DB97774FE2E8&userid=180479389&__reqTraceID=75259068-0bc3-4619-90e9-c1d7d01f1f4c&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1474796538636&__skua=58810434ff9b9a0bc6d4edd46aea3499&__skno=25d1f269-4af2-4f59-8fed-f979daef2ae9&__skcy=tMe4QWSIA4HSoIrORKgdap29Rp8%3D";
     */
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    dictM[@"supportId"]=@1;
    NSString *url = @"http://api.meituan.com/group/v2/recommend/homepage/city/76?userId=180479389&position=38.042167%2C114.477345&wifi-cur=0&wifi-mac=88%3A25%3A93%3A93%3A4e%3A9d%0888%3A25%3A93%3A93%3A4e%3A9c%0824%3A69%3A68%3Ae4%3Ae1%3A4f%0888%3A25%3A93%3A93%3A4e%3A1d%08&wifi-name=TP-LINK_1505%08TP-LINK_1505%2B%08TP-LINK_1503%08WYZC1506%08&wifi-strength=-44%08-54%08-52%08-65%08&fields=imageUrl%2Ctitle%2CimageTitle%2CsubTitle%2CsubTitle2%2CmainMessage%2CmainMessage2%2CsubMessage%2CtopRightInfo%2CbottomRightInfo%2C_type%2C_from%2C_id%2C_iUrl%2C_jumpNeed%2Ccolor%2Ccampaign%2CglobalId&client=android&__vhost=api.mobile.meituan.com&utm_source=huawei&utm_medium=android&utm_term=431&version_name=7.3.1&utm_content=a0000055de24e8&utm_campaign=AgroupBgroupC0D200E0&ci=76&msid=a0000055de24e81474794199082&uuid=FDC1C5960287691C1AA0FDB6CE8736766612182AEF7A556CA2D3DB97774FE2E8&userid=180479389&__reqTraceID=75259068-0bc3-4619-90e9-c1d7d01f1f4c&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1474796538636&__skua=58810434ff9b9a0bc6d4edd46aea3499&__skno=25d1f269-4af2-4f59-8fed-f979daef2ae9&__skcy=tMe4QWSIA4HSoIrORKgdap29Rp8%3D";
    
    [manger GET:url parameters:dictM success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //写成桌面plist文件
        //[responseObject writeToFile:@"/Users/anlin/Desktop/three.plist" atomically:YES];
        
        //请求数组（请求网络上的数据）
        NSArray *array = responseObject[@"data"];
        for (NSDictionary *dict in array) {
            ALThreeModel *model = [ALThreeModel threeModelWithDict:dict];
            [self.arrM addObject:model];
        }
        //NSLog(@"ddd%@",self.arrM);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}


#pragma mark 当点击“城市”的时候进入下一界面
- (void)cityClick{
    
    TLCityPickerController *cityPickerVC = [[TLCityPickerController alloc] init];
    [cityPickerVC setDelegate:self];
    
    cityPickerVC.locationCityID = @"1400010000";
    
    cityPickerVC.hotCitys = @[@"100010000", @"200010000", @"300210000", @"600010000", @"300110000"];
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
        
    }];
    
}


#pragma mark 当点击"二维码"的时候进入下一界面
- (void)sweep{
    
    QRCodeViewController *code = [[QRCodeViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:code];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark 当点击“右上角”的时候进入下一界面
- (void)readMessage{
    
    ALMessageController *message = [[ALMessageController alloc]init];
    UINavigationController *na =  [[UINavigationController alloc]initWithRootViewController:message];
    [self presentViewController:na animated:YES completion:nil];
}

#pragma mark 当点击“搜索栏”的时候进入下一界面
- (void)searchMessage{

    ALSearchController *search = [[ALSearchController alloc]init];
    [self presentViewController:search animated:YES completion:nil];
    
//    SearchDisPlayerController *search = [[SearchDisPlayerController alloc]init];
//       [self presentViewController:search animated:YES completion:nil];
}



#pragma mark - TLCityPickerDelegate
- (void) cityPickerController:(TLCityPickerController *)cityPickerViewController didSelectCity:(TLCity *)city
{
    
    [self.city setTitle:city.cityName forState:UIControlStateNormal];
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void) cityPickerControllerDidCancel:(TLCityPickerController *)cityPickerViewController
{
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}







@end
